package cn.alusiaor.eurekaclientarticleservice.controller;

import cn.alusiaor.eurekaclientarticleservice.component.TaskService;
import cn.alusiaor.eurekaclientarticleservice.exception.CustomException;
import cn.alusiaor.eurekaclientarticleservice.exception.response.ExampleCustomExceptionEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {

    @Autowired
    private TaskService taskService;


    @Value("${cloud.config.name}")
    private String appName;

    @Value("${client.config.name}")
    private String heheName;

    @PostMapping("/callHello/post")
    public UserDto callHelloPost(@RequestBody UserDto userDto) {
        String callStr = taskService.doTaskOne(userDto);
        log.info("这里是我返回的数据" + callStr);
        return userDto;
    }

    @GetMapping("/callHello/get")
    /**
     * http://localhost:55001/article/callHello/get?userName=Hello&createTime=2022-05-04&birthday=2022-05-04%2011:40:34&modifyTime=2022-05-04
     */
    public UserDto callHelloGet(@RequestParam("userName") String userName, @RequestParam("modifyTime") LocalDate modifyTime, @RequestParam("createTime") Date createTime, @RequestParam("birthday") LocalDateTime birthday) {
        UserDto userDto = new UserDto(userName, birthday, createTime, modifyTime);
        String callStr = taskService.doTaskOne(userDto);
        log.info("这里是我返回的数据" + callStr);
        return userDto;
    }


    @GetMapping("/config")
    public String getConfig() {
        log.info("这里是我配置的内容" + appName);
        log.info("这里是我配置的内容" + heheName);
        return appName + ";" + heheName;
    }


    @GetMapping("/config/void")
    public void getVoid() {
        log.info("这里是我配置的内容" + appName);
        log.info("这里是我配置的内容" + heheName);
    }

    @GetMapping("/config/err")
    public ResponseEntity geterr() {
        log.info("这里是我配置的内容" + appName);
        log.info("这里是我配置的内容" + heheName);
        throw new RuntimeException("错误");
    }

    @GetMapping("/config/enum")
    public ResponseEntity getenum() {
        log.info("这里是我配置的内容" + appName);
        log.info("这里是我配置的内容" + heheName);
        throw new CustomException(ExampleCustomExceptionEnum.ERROR_REQUEST);
    }


//    @PostMapping("/file/upload")
//    public File getFile(@RequestParam("file") MultipartFile file) throws IOException {
//        File file1 = userService.getFile(file);
//        InputStream inputStream = new FileInputStream(file1);
//        String originalFilename = file.getOriginalFilename();
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//        Files.copy(inputStream, Paths.get("133666" + substring));
//
//        return file1;
//    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class UserDto {
        private String userName;
        private LocalDateTime birthday;
        private Date createTime;
        private LocalDate modifyTime;
    }

}
